/*启动页*/
import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:frametrim/app/app_navigator.dart';
import 'package:frametrim/route/Routes.dart';
import 'package:frametrim/utility/dataServer.dart';
import 'package:frametrim/utility/screen.dart';
import 'package:frametrim/utility/sp_utils.dart';
import 'package:get/get.dart';
import 'loginPage.dart';
import 'mainPage.dart';

class SplashPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => SplashState();
}

class SplashState extends State<SplashPage> {
  late Timer _timer;
  late String token;

  //注入依赖方式一
  // final _splashController = Get.put(SplashController());

  @override
  void initState() {
    super.initState();
    //设置状态栏不显示
    SystemChrome.setEnabledSystemUIOverlays([]);
    getToken();
  }

  getToken() {
    //倒计时跳转
    _timer = Timer.periodic(Duration(seconds: 3), (timer) {
      setState(() {
        token = SPUtil.getString(DataServer.mUserToken)!;
        if (token.isNotEmpty) {
          Get.offNamed(Routes.MAIN);
          // AppNavigator.pushAndRemove(context, MainPage());
        } else {
          Get.offNamed(Routes.LOGIN);
          // AppNavigator.pushAndRemove(context, LoginPage());
        }
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Image.asset(
        'image/qd1.png',
        width: Screen.width,
        height: Screen.height,
        fit: BoxFit.fill,
      ),
    );
  }

  @override
  void dispose() {
    _timer.cancel();
    //恢复状态栏和导航栏   不恢复的话后面的页面也没有状态栏展示
    SystemChrome.setEnabledSystemUIOverlays(SystemUiOverlay.values);
    super.dispose();
  }
}
